<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('添加图片')" />
    <th:block th:include="include :: cropbox-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-assetDetail-addImg">
            <input name="id" th:value="${id}" type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label">图片：</label>
                <div class="imageBox">
                    <div class="thumbBox"></div>
                    <div class="spinner" style="display: none">Loading...</div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">   </label>
                <div class="new-contentarea tc">
                    <a href="javascript:void(0)" class="upload-img"> <label for="avatar">上传图像</label> </a>
                    <input type="file" class="" name="" id="avatar" accept="image/*"/>
                    <input name="path" type="hidden" id="imgUrl">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: cropbox-js" />
    <script type="text/javascript">
        var prefix = ctx + "asset/assetDetail";
        var options = {
            thumbBox: '.thumbBox',
            spinner: '.spinner',
            imgSrc:""
        }
        var imgSrc=options.imgSrc;
        $(function () {
            cropper = $('.imageBox').cropbox(options);
            $('#avatar').on('change', function() {
                var reader = new FileReader();
                reader.onload = function(e) {
                    options.imgSrc = e.target.result;
                    //根据MIME判断上传的文件是不是图片类型
                    if((options.imgSrc).indexOf("image/")==-1){
                        $.modal.alertWarning("文件格式错误，请上传图片类型,如：JPG，PNG后缀的文件。");
                    } else {
                        cropper = $('.imageBox').cropbox(options);
                        $('.imageBox').css("display","block");
                    }
                }
                reader.readAsDataURL(this.files[0]);
            })
        })

        function submitHandler() {
            var img = cropper.getBlob();
            var formdata = new FormData();
            formdata.append("avatarfile", img);
            $.ajax({
                url: prefix+ "/updateImgUrl",
                data: formdata,
                type: "post",
                processData: false,
                contentType: false,
                success: function(result) {
                    $("#imgUrl").val(result.msg);
                    options.imgSrc=ctx + 'profile/upload/' +result.msg;
                    if ($.validate.form()) {
                        $.operate.save(prefix + "/addImgSave", $('#form-assetDetail-addImg').serialize());
                    }
                }
            })
        }
    </script>
</body>
</html>